// pages/pallet_details/pallet_details.js
var app = getApp();
var util = require('../../util.js');
var priceMap = {};//价格组，传给后台
Page({

  /**
   * 页面的初始数据
   */
  data: {
    pallet_select: false,
    options: 0,
    options_value: '5T',
    time: '00:00',
    date: "2018-1-1",
    price:false,//价格显示
    model: [],
    price2group: [],//价格组，用于判断格式
  },
  // 获取货盘信息
  getPalletInfoById: function (res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/sys/cargo/' + that.data.options.palletid,
        data: {
          token: session
        },
        success: function (res) {
          var res = res.data;
          if (res.msg == "OK") {
            var data = res.data;
            that.setData({
              cargo: data.cargo,
              transportVo: data.transportVo,
              price4Group2: data.price4Group2,
              sendDate1: util.formatDate(new Date(data.cargo.sendDate1), 'YYYY-MM-dd'),
              sendDate2: util.formatDate(new Date(data.cargo.sendDate2), 'YYYY-MM-dd'),
              loading: false
            })

            var price2group = that.data.price2group;
            var price4Group2 = that.data.price4Group2;

            for (var i = 0; i < price2group.length; i++) {
              for (var j = 0; j < price4Group2.length; j++) {
                if (price4Group2[j].name == price2group[i].name){
                  if (price4Group2[j].price){
                    price2group[i].price = price4Group2[j].price;
                  }
                }
              }
            }
            that.setData({
              price2group: price2group
            })
          } else {
            that.setData({
              loading: false,
              loadtips: res.msg
            })
          }
        },
        fail: function (res) {
          that.setData({
            loading: true
          })
        }
      })
    })
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setData({
      options: options
    });
    // 授权
    app.getLoginSession(function (session) {

    //获取车型
      wx.request({
        url: app.api + "/wx_log_test/sys/vehicle?token=" + session,
        success: function (res) {
          var model = that.data.model;
          for (var i = 0; i < res.data.data.length; i++) {
            model.push(res.data.data[i].value);
            that.setData({
              model: model
            })
          }
        }
      });

      //获取分组
      wx.request({
        url: app.api + '/wx_log_test/sys/group',
        data: {
          token: session
        },
        success: function (res) {
          var res = res.data;
          if (res.msg == "OK") {
            var data = res.data;
            var price2group = that.data.price2group;
            for (var i = 0; i < data.length; i++) {
              price2group.push({ id: data[i].id, price: '', name: data[i].name });
              that.setData({
                price2group: price2group
              })
            }
          }
        }
      })
    })
    that.getPalletInfoById();
    if(that.data.options.status==2){
      that.driverdetail();
    }
  },
  bindDateChange: function (e) {
    this.setData({
      date: e.detail.value
    })
  },
  bindTimeChange: function (e) {
    this.setData({
      time: e.detail.value
    })
  },

  optionsSelect: function (e) {
    var cur = e.currentTarget.dataset.cur;
    var value = e.currentTarget.dataset.value;
    this.setData({
      options: cur,
      options_value: value,
    })
  },
  // 实时获取用户输入
  input: function (event) {
    var that = this;
    var value = event.detail.value;
    var type = event.currentTarget.dataset.type;
    if (type == 'remain') {
      that.data.cargo.remain = value;
      that.setData({
        cargo: that.data.cargo
      })
    } else if (type == 'remark') {
      that.data.cargo.remark = value;
      that.setData({
        cargo: that.data.cargo
      })
    } else if (type == 'length') {
      that.data.cargo.length = value;
      that.setData({
        cargo: that.data.cargo
      })
    } else if (type == 'payload') {
      that.data.cargo.payload = value;
      that.setData({
        cargo: that.data.cargo
      })
    } else if (type == 'volume') {
      that.data.cargo.volume = value;
      that.setData({
        cargo: that.data.cargo
      })
    } else if (type == 'goodaprice') {
      for (var i = 0; i < that.data.price2group.length; i++) {
        if (that.data.price2group[i].id == event.currentTarget.dataset.id) {
          that.data.price2group[i].price = value;
          that.setData({
            price2group: that.data.price2group
          })
        }
      }
    }
  },
  //车型
  changemodel: function (e) {
    this.data.cargo.vehicle=this.data.model[e.detail.value]
    this.setData({
      modelindex: e.detail.value,
      cargo:this.data.cargo
    })
  },
  // 用户输入验证
  virifi: function () {
    var that = this;
    var error = '';
    var options = that.data.options;
    for (var i = 0; i < that.data.price2group.length; i++) {
      priceMap[that.data.price2group[i].id] = that.data.price2group[i].price;//由于后台对价格组的要求，所以再赋值一次
    }
    if (that.data.cargo.remain == null || that.data.cargo.remain == undefined || that.data.cargo.remain.length == 0 || !/^\d+(\.\d+)?$/.test(that.data.cargo.remain)) {
      error = '请输入正确的余量';
    } else if (that.data.cargo.length != null && that.data.cargo.length.length != 0 && !/^\d+(\.\d+)?$/.test(that.data.cargo.length)) {
      error = '请输入正确的车长';
    } else if (that.data.cargo.payload != null && that.data.cargo.payload.length != 0 && !/^\d+(\.\d+)?$/.test(that.data.cargo.payload)) {
      error = '请输入正确的载重';
    } else if (that.data.cargo.volume != null && that.data.cargo.volume.length != 0 && !/^\d+(\.\d+)?$/.test(that.data.cargo.volume)) {
      error = '请输入正确的体积';
    } else {
      console.log(that.data.price2group);
      for (var i = 0; i < that.data.price2group.length; i++) {
        if (that.data.price2group[i].name == '默认组') {
          if (that.data.price2group[i].price == '' || !/^\d+(\.\d+)?$/.test(that.data.price2group[i].price)) {
            error = '请输入正确的运单价';
          }
        } else {
          if (that.data.price2group[i].price != '' && !/^\d+(\.\d+)?$/.test(that.data.price2group[i].price)) {
            error = '请输入正确的运单价';
          }
        }
      }
    }
    return error;
  },
  bindSelect: function (e) {
    var cur = e.currentTarget.dataset.cur;
    this.setData({
      pallet_select: cur,
    })
  },

  call: function (res) {
    wx.makePhoneCall({
      phoneNumber: res.currentTarget.dataset.num //仅为示例，并非真实的电话号码
    })
  },

  // 更新货盘信息
  updatePallet: function (res) {
    var that = this;
    var virify = that.virifi();

    var status = res.currentTarget.dataset.status;
    if (virify != '') {
      wx.showModal({
        title: '提示',
        content: virify,
        showCancel: false
      });
      return false;
    };

    // 增加关闭货盘及货盘完结提示
    var goNext = false;
    if (status == -1 || status == 2){
      var content = status == -1 ? '确认关闭货盘？' : '确认货盘完结？'
      wx.showModal({
        title: '提示',
        content: content,
        success: function(confirmResult){
          if (confirmResult.confirm){
            var goNext = true;
          }
        }
      })
    }else{
      var goNext = true;
    }

    if (goNext){
      app.getLoginSession(function (session) {
        wx.request({
          url: app.api + '/wx_log_test/sys/cargo?token=' + session,
          method: 'PUT',
          header: {
            'content-type': 'application/json'
          },
          data:{
            cargo:that.data.cargo,
            price2group: priceMap
          },
          success: function (res) {
            var res = res.data;
            if(res.msg=="OK"){
              wx.request({
                url: app.api + '/wx_log_test/cargo/change',
                data: {
                  status:status,
                  id: that.data.options.palletid,
                  token: session
                },
                success: function (res) {
                  var res = res.data;
                  if (res.msg == "OK") {
                    wx.navigateBack({
                      delta: 1
                    })
                  } else {
                    wx.showToast({
                      title: '请重试！',
                      icon: 'loading'
                    })
                  }
                },
                fail: function (res) {
                  wx.showToast({
                    title: '请重试！',
                    icon: 'loading'
                  })
                }
              })
            }else{
              wx.showToast({
                title: '请重试！',
                icon: 'loading'
              })
            }
          },
          fail: function (res) {
            wx.showToast({
              title: '请重试！',
              icon: 'loading'
            })
          }
        })
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //显示价格
  showprice:function(){
    var that = this;
    that.setData({
      price:that.data.price?false:true
    })
  },
  //获取接单信息
  driverdetail:function(res){
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api + '/wx_log_test/order/cargo/',
        data: {
          token:session,
          cargoId:that.data.options.palletid,
          page:1,
          perpage:10000000
        },
        success:function(res){
          var res = res.data;
          if(res.msg=="OK"){
            that.setData({
              driver:res.data.content
            })
          }
        }
      })
    })
    
  }
})